<?php

if(!defined('sugarEntry'))define('sugarEntry', true);
require_once('include/nusoap/nusoap.php'); 

/**
 * Sugar Soap API Class
 * 
 * @author Zidong Cai
 *
 */
class Sugar_Soap{


  private $session;
  
  private $version = '.01';
  
  private $soapClient;

  /**
   * Construct function of Sugar_Soap
   * 
   * @param string $soapURL
   * @return 
   */
  public function __construct($soapURL){

    $this->soapClient = new nusoapclient($soapURL); 

  }


  /**
   * Log into the SugarCRM 
   * 
   * @param string $userName
   * @param string $userPass
   * @return
   * @throws Exception
   */
  public function login($userName, $userPass){


    $version = $this->version;
    $result = $this->soapClient->call('login',array('user_auth'=>array('user_name'=>$userName,'password'=>md5($userPass), 'version'=>$version), 'application_name'=>'SoapTest'));
    if($result['error']['number'] == 0){
      $this->session = $result['id'];
    }
    else{
      throw new Exception($this->soapClient->error_str);
    }

  }

  /**
   * Log out of the SugarCRM 
   * 
   * @return
   * @throws Exception
   */
  public function logout(){

    $result = $this->soapClient->call('logout',array('session'=>$this->session));

    if($result['number'] != 0){
      throw new Exception($this->soapClient->error_str);
    }

  }

  /**
   * Retrieve current session id
   * 
   * @return string
   */
  public function getSessionId(){

    return $this->session? $this->session : '';
  }


  /**
   * Retrieve data from specific module
   *
   * @param string $moduleName
   * @param string $query
   * @param string $orderBy
   * @param array $selectFields
   * @return array
   * @throws Exception
   */
  public function getEntryList($moduleName,   $selectFields = array(),  $query = '', $orderBy = '', $offset = 0, $maxResult = 100){


    $session = $this->session;

    $result = $this->soapClient->call('get_entry_list',array('session'=>$session,'module_name'=>$moduleName,'query'=>$query,'order_by'=> $orderBy,'offset'=>$offset, 'select_fields'=>$selectFields, 'max_results'=>$maxResult));
    if($result['error']['number'] == 0 && $result != false){
      return $result;
    }
    else{
      throw new Exception($this->soapClient->error_str);
    }


  }


  /**
   * Get the values of a record.
   *
   * @param string $moduleName
   *
   * @param array $selectFields
   * @param string $id
   * @return $result
   * @throws Exception
   */
  public function getEntry($moduleName, $selectFields = array(), $id  = '0'){


    $session = $this->session;

    $result = $this->soapClient->call('get_entry', array('session' => $session, 'module_name' => $moduleName, 'id' => $id, 'select_fields'=>$selectFields));

    if($result['error']['number'] == 0 && $result != false){
      return $result;
    }
    else{
      throw new Exception($this->soapClient->error_str);
    }
  }





  /**
   * Create or update an entry in SugarCRM
   *
   * @param  string $module_name
   * @param  array $name_value_list
   * @return $result
   * @throws Exception
   */
  public function setEntry($module_name, $name_value_list = array()){


    $set_entry_params = array(

              'session' => $this->session,
              'module_name' => $module_name,
              'name_value_list' => $name_value_list,

    );

    $result = $this->soapClient->call('set_entry', $set_entry_params);

    if($result['error']['number'] == 0 && $result != false){
      return $result;
    }
    else{
      throw new Exception($this->soapClient->error_str);
    }
  }


  /**
   * Create a new entry in SugarCRM
   * @param string $module_name
   * @param array $name_value_list
   * @return
   * @throws Exception
   */
  public function creatEntry($module_name, $name_value_list = array()){

    $result = $this->setEntry($module_name, $name_value_list);
    return $result;
  }

  /**
   * Update an exisitng entry 
   * 
   * @param string $module_name
   * @param string $id
   * @param array $name_value_list
   * @return
   * @throws Exception
   */
  public function updateEntry($module_name, $id, $name_value_list = array()){

    $name_value_list[] = array('name' => 'id' ,'value' => $id);
    $result = $this->setEntry($module_name, $name_value_list);
    return $result;
  }


  /**
   * Get the current logging user's id
   *
   * @return string
   * @throws Exception
   */
  public function getUserId(){

    $result = $this->soapClient->call('get_user_id', $this->session);

    if($result){
      return $result;
    }
    else{
      throw new Exception($this->soapClient->error_str);
    }


  }


}
